;(function($) {
	$.fn.tree = function(options){
		if (typeof options == 'string') {
			/*将参数中的第一个参数去掉，剩下的作为参数传入方法调用*/
			var args = Array.prototype.slice.call(arguments, 1);
			var res = undefined;
			this.each(function() {
				/*判断传入的方法是否有定义，如果有，在当前的jQuery对象上进行调用*/
				if ($.isFunction($.fn.tree.methods[options])) {
					var r = $.fn.tree.methods[options].apply(this, args);
					if (res === undefined) {
						res = r;
					}
				}
			});
			return res;
		
		} else {
			_that = this;
			var defalut = {
				view: {
	                addHoverDom: _containMethod(options, "addHoverDom", function() {}),
	                removeHoverDom: _containMethod(options, "removeHoverDom", function() {}),
	                selectedMulti: false
	            },
				async : {
					enable : true,
					url : _contain(options, "url", "/query")
				// otherParam: {"roleCode": parent.getData().roleCode}
				},
				check : {
					enable : _contain(options, "check", true),
				},
				data : {
					key : {
						name : _contain(options, "name", "name"),
					},
					simpleData : {
						enable : _contain(options, "data", true),
						idKey : _contain(options, "id", "id"),
						pIdKey : _contain(options, "pId", "pId"),
						rootPId : _contain(options, "rootPId", "larry"), // 用于修正根节点父节点数据，即
																			// pIdKey
																			// 指定的属性值
					}
				},
				edit : {
					enable : _contain(options, "edit", false),
				},
				// 回调函数
				callback : {
					onAsyncSuccess : _containMethod(options, "onAsyncSuccess",
							_onAsyncSuccess),
					onClick : _containMethod(options, "onClick", function() {
					}),
					onRightClick : _containMethod(options, "onRightClick",function() {
							
					})
				}
			};

			$(this).each(function() {
				var _this = $(this);
				$.fn.zTree.init(_this, $.extend({}, defalut, options));
				_this.prepend('<input type="text" class="form-control input" id="'+_this.attr("id")+'_search" name="'+_this.attr("id")+'_search" style="width:203px;margin-bottom:6px;padding-left:10px;padding-right:10px" autocomplete="off">');
				$("#"+_this.attr("id")+'_search').keypress(function(event) {
						 _nodeList = [];
						 var treeObj = $.fn.zTree.getZTreeObj(_this.attr("id"));
						 var value = $("#"+_this.attr("id")+'_search').val();
						var nodes = treeObj.getNodes();
						var allNode = treeObj.transformToArray(nodes);
						treeObj.hideNodes(allNode);
						_nodeList = treeObj.getNodesByParamFuzzy(_contain(options, "name", "name"), value, null);
						
						for ( var n in _nodeList) {
							_findParent(treeObj, _nodeList[n]);
						}
						treeObj.showNodes(_nodeList);
						if (value == "") {
							treeObj.expandAll(false);
							treeObj.expandNode(nodes[0], true, false);
							var nodeLeaf = _getFirstLeafNodes(nodes[0]);//查找到第一个叶子节点
							treeObj.expandNode(nodeLeaf.getParentNode(), true, false);
						}
				});
			});
		}

	};
	
	$.fn.tree.methods = {
			firstLeafNode:function(treeNode)
			{
				return _getFirstLeafNodes(treeNode);
			}
	};
	
	var  _nodeList = [];
	function _contain(option,key,defaultkey){
		return option !=null&&option.hasOwnProperty(key)?option[key]:defaultkey
	}
	function _containMethod(option ,fn,defaultfn){
		if(option !=null&&option.hasOwnProperty(fn)&&option[fn] && $.isFunction(option[fn])){
			return option[fn]
		}else{
			return defaultfn;
		}
		 
	}
	function _getFirstLeafNodes(treeNode){
		if (treeNode.isParent) {
			 var childrenNodes = treeNode.children;
			 if (childrenNodes) {
				return  _getFirstLeafNodes(childrenNodes[0]);
			 }
		}else{
			return treeNode;
		}
		
	}
	
	
	
	function _onAsyncSuccess(event, treeId){
		var zTree = $.fn.zTree.getZTreeObj(treeId);
		var nodeList = zTree.getNodes();
		zTree.expandNode(nodeList[0], true);
		var nodes = zTree.getCheckedNodes();
		for (var i = 0, l = nodes.length; i < l; i++) {
			zTree.checkNode(nodes[i], true, true);
		}
	}
	
	
	function _findParent(treeObj, node) {
		treeObj.expandNode(node, true, false, false);
		var pNode = node.getParentNode();
		if (pNode != null) {
			_nodeList.push(pNode);
			_findParent(treeObj, pNode);
		}
	}
	
})(jQuery);
